package demo;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.operators.Order;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.*;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * 老的写法，已经淘汰。
 */
public class Flink01_WordCount {



    public static void main(String[] args) throws Exception {

        /**
         * 创建执行环境
         */

        ExecutionEnvironment executionEnvironment = ExecutionEnvironment.getExecutionEnvironment();

        DataSource<String> stringDataSource = executionEnvironment.readTextFile("data/word.txt");


        FlatMapOperator<String, Tuple2<String, Integer>> stringTuple2FlatMapOperator = stringDataSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] s1 = s.split(" ");

                for (String s2 : s1) {
                    Tuple2<String, Integer> tuple2 = new Tuple2<>(s2, 1);
                    out.collect(tuple2);
                }

            }
        });

        UnsortedGrouping<Tuple2<String, Integer>> tuple2UnsortedGrouping = stringTuple2FlatMapOperator.groupBy(0);


        AggregateOperator<Tuple2<String, Integer>> sum = tuple2UnsortedGrouping.sum(1);
        sum.print();
        System.out.println("*************************************************************************************************************************************************");

        SortPartitionOperator<Tuple2<String, Integer>> tuple2SortPartitionOperator = sum.sortPartition(0, Order.ASCENDING);

        tuple2SortPartitionOperator.print();


    }
}
